<template>
    <div class="list">
        <div class="table" v-if="active===0">
            <div class="header">
                <el-page-header @back="goBack" content="案件页面" style="position: relative;top: 10px;left: 5px;">
                </el-page-header>
            </div>
            <el-divider></el-divider>
            <div class="card">
                <el-tabs v-model="params.tab" type="card" @tab-click="handleSearch">
                    <el-form :model="params" style="width: 100%;">
                        <el-container>
                            <!--内容-->
                            <el-form-item>
                                <el-input v-model="params.content" style="width: 200px;"></el-input>
                            </el-form-item>
                            <!--字段类型-->
                            <el-form-item style="margin-left: 10px">
                                <el-select v-model="params.type" :value="params.type" style="width: 200px;">
                                    <el-option
                                            v-for="type in typeList"
                                            :label="type.value"
                                            :value="type.key"
                                            :key="type.key"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                            <!--审批状态-->
                            <el-form-item style="margin-left: 10px">
                                <el-select v-model="params.status" :value="params.status" style="width: 130px;">
                                    <el-option value="" label="收案状态"></el-option>
                                    <el-option value="1" label="审批中"></el-option>
                                    <el-option value="2" label="审批不通过"></el-option>
                                    <el-option value="3" label="审批通过"></el-option>
                                </el-select>
                            </el-form-item>
                            <!--案件类型-->
                            <el-form-item style="margin-left: 10px">
                                <el-select v-model="params.caseType" :value="params.caseType" style="width: 150px;">
                                    <el-option label="案件类型" value=""></el-option>
                                    <el-option
                                            v-for="caseType in caseTypeList"
                                            :label="caseType.caseTypeName"
                                            :value="caseType.id"
                                            :key="caseType.id"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                            <!--结案状态-->
                            <el-form-item style="margin-left: 10px">
                                <el-select v-model="params.settleStatus" :value="params.settleStatus" style="width: 120px;">
                                    <el-option value="" label="结案状态"></el-option>
                                    <el-option value="Y" label="已结案"></el-option>
                                    <el-option value="N" label="未结案"></el-option>
                                </el-select>
                            </el-form-item>
                            <!--自定义排序-->
                            <el-form-item style="margin-left: 10px">
                                <el-select v-model="sortModel" :value="sortModel" @change="handleSort">
                                    <el-option label="自定义排序" value=""></el-option>
                                    <el-option
                                            v-for="sort in sortList"
                                            :label="sort.value"
                                            :value="sort.value"
                                            :key="sort.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item style="margin-left: 10px">
                                <el-button type="primary" @click="handleSearch">检索</el-button>
                            </el-form-item>
                        </el-container>
                    </el-form>

                    <el-tab-pane label="我的个人案件" name="own">
                        <case-table :laws="pageResult.records" @reload="reload" @cost="cost" @close="close" @inout="inout" @invoice="invoice" @replace="replace"></case-table>
                    </el-tab-pane>
                    <el-tab-pane label="我的合办案件" name="cooperation">
                        <case-table :laws="pageResult.records" @reload="reload" @cost="cost" @close="close" @inout="inout" @invoice="invoice" @replace="replace"></case-table>
                    </el-tab-pane>
                    <el-tab-pane label="共享案件" name="share">
                        <case-table :laws="pageResult.records" @reload="reload" @cost="cost" @close="close" @inout="inout" @invoice="invoice" @replace="replace"></case-table>
                    </el-tab-pane>
                    <el-tab-pane label="我的待收款案件" name="unpaidsal">
                        <case-table :laws="pageResult.records" @reload="reload" @cost="cost" @close="close" @inout="inout" @invoice="invoice" @replace="replace"></case-table>
                    </el-tab-pane>
                    <el-tab-pane label="我的案件汇总" name="total">
                        <case-table :laws="pageResult.records" @reload="reload" @cost="cost" @close="close" @inout="inout" @invoice="invoice" @replace="replace"></case-table>
                    </el-tab-pane>
                </el-tabs>

                <el-footer class="case-list-footer">
                    <el-pagination
                            style="margin: 10px auto"
                            background
                            layout="prev, pager, next"
                            :total="pageResult.total"
                            :page-size="pageResult.size"
                            class="pagination"
                            @current-change="handleIndexChange"
                    >
                    </el-pagination>
                </el-footer>
            </div>
        </div>

        <!--费用减免-->
        <div class="cost" v-if="active===1">
            <feiyong-add @back="back" :law="law" :active="active" @completed="handleCompleted"></feiyong-add>
        </div>
        <!--结案-->
        <div class="close" v-if="active===2">
            <case-handle @back="back" :law="law" :active="active" @completed="handleCompleted"></case-handle>
        </div>
        <!--添加收款记录-->
        <div class="inout" v-if="active===3">
            <law-inout @back="back" :law="law" :active="active" @completed="handleCompleted"></law-inout>
        </div>
        <!--开发票的页面-->
        <div class="inout" v-if="active===4">
            <law-invoice @back="back" :law="law" :active="active" @completed="handleCompleted"></law-invoice>
        </div>
    </div>
</template>

<script>
    //引入个人案件组件
    const CaseTable = () => import('./CaseTable.vue')
    const FeiyongAdd = () => import('../Feiyong/FeiyongAdd.vue')
    const CaseHandle = () => import('./CaseHandle.vue')
    const LawInout = () => import('./LawInout.vue')
    const LawInvoice = () => import('./LawInvoice.vue')
    //引入案件类型服务类
    import CaseTypeService from '../../../model/gs/CaseTypeService.js'
    import CaseLawService from '../../../model/gs/CaseLawService.js'

    const caseTypeService = CaseTypeService.getInstance()
    const caseLawService = CaseLawService.getInstance()

    export default {
        data() {
            return {
                //控制跳转的变量
                active:0,
                //表格选中的一行的对象
                law:{},
                pageResult:{},
                params:{
                    type:'case_wtr/case_suspect',
                    status:'',
                    caseType:'',
                    settleStatus:'',
                    tab:'own',
                    isOwn:'1',
                    recycle:'N'
                },
                caseTypeList:[],
                typeList:[
                    {
                        key:'case_wtr/case_suspect',
                        value:'委托人/嫌疑人搜索'
                    },
                    {
                        key:'case_opposite_parties',
                        value:'对方当事人搜索'
                    },
                    {
                        key:'case_no',
                        value:'案号搜索'
                    },
                    {
                        key:'case_cause',
                        value:'案由搜索'
                    },
                    {
                        key:'case_attorney',
                        value:'承办律师搜索'
                    },
                    {
                        key:'case_accept',
                        value:'受理机关搜索'
                    }
                ],
                sortModel:'',
                sortList:[
                    {
                        key:'case_subtime',
                        value:'登记时间从高到低',
                        type:'desc'
                    },
                    {
                        key:'case_subtime',
                        value:'登记时间从低到高',
                        type:'asc'
                    },
                    {
                        key:'collection_time',
                        value:'收案时间从高到低',
                        type:'desc'
                    },
                    {
                        key:'collection_time',
                        value:'收案时间从低到高',
                        type:'asc'
                    },
                    {
                        key:'id',
                        value:'案号从高到低',
                        type:'desc'
                    },
                    {
                        key:'id',
                        value:'案号从低到高',
                        type:'asc'
                    }
                ]
            }
        },
        methods:{
            //返回上一个路由的方法
            goBack(){
                history.back()
            },
            back(){
                this.active = 0
            },
            //子组件处理完成的回调方法
            handleCompleted(){
                this.getCaseLaws()
                this.active = 0
            },
            //重新立案的方法
            replace(law){
                caseLawService.getById(law.id).then(response => {
                    if(response.data.code === 0){
                        const law =  response.data.data
                        //查询出最大编号并赋值
                        caseLawService.getCode().then(response1 => {
                            if(response1.data.code === 0){
                                law.caseNo = law.caseNo.substring(law.caseNo.indexOf('('),law.caseNo.indexOf(')')+1)
                                let num = response1.data.data
                                if(num < 10){
                                    num = '0'+num
                                }
                                law.caseNo =
                                    new Date().getFullYear()
                                    +law.caseNo+'第00'
                                    +num+'号'
                                //跳转路由
                                this.$router.push({
                                    name:'Choice',
                                    params:{
                                        active:3,
                                        cost:0,
                                        law
                                    }
                                })
                            }
                        })
                    }
                })
            },
            //开发票的方法
            invoice(law){
                caseLawService.getById(law.id).then(response => {
                    if(response.data.code === 0){
                        this.law = response.data.data
                        this.active = 4
                    }
                })
            },
            //添加收款记录的方法
            inout(law){
                caseLawService.getById(law.id).then(response => {
                    if(response.data.code === 0){
                        this.law = response.data.data
                        this.active = 3
                    }
                })
            },
            //费用减免的方法
            cost(law){
                caseLawService.getById(law.id).then(response => {
                    if(response.data.code === 0){
                        this.law = response.data.data
                        this.active = 1
                    }
                })
            },
            //检索按钮点击事件
            handleSearch(){
                //重置当前页为1
                this.params.page = 1
                this.getCaseLaws()
            },
            //获取案件的方法
            getCaseLaws(){
                caseLawService.getByPage(this.params).then(response => {
                    if(response.data.code === 0){
                        this.pageResult = response.data.data
                    }
                })
            },
            //获取案件类型的方法
            getCaseTypeList(){
                caseTypeService.parentList().then(response => {
                    if(response.data.code === 0){
                        this.caseTypeList = response.data.data
                    }
                })
            },
            //当前页放生变化的事件
            handleIndexChange(index){
                this.params.page = index
                this.getCaseLaws()
            },
            //排序的方法
            handleSort(){
                if(this.sortModel == ''){
                    this.params.sortRule = ''
                    this.params.sortColumn = ''
                    return
                }
                this.sortList.forEach(item => {
                    if(item.value == this.sortModel){
                        this.params.sortRule = item.type
                        this.params.sortColumn = item.key
                    }
                })
            },
            //结案的方法
            close(law){
                caseLawService.getById(law.id).then(response => {
                    if(response.data.code === 0){
                        this.law = response.data.data
                        this.active = 2
                    }
                })
            },
            //刷新的方法
            reload(){
                this.getCaseLaws()
            }
        },
        created(){
            this.getCaseTypeList()
            this.getCaseLaws()
        },
        components:{
            CaseTable,
            FeiyongAdd,
            CaseHandle,
            LawInout,
            LawInvoice
        }
    }
</script>

<style scoped>
    .list{
        width: 100%;
    }
    .header{
        width: 100%;
        font-size: 20px;
        height: 30px;
        line-height: 30px;
        background-color: white;
    }
    .card{
        width: 100%;
    }
    .case-list-footer{
        width: 100%;
        height: 10%;
        position: relative;
    }
    .case-list-footer .pagination{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
    }
    .table{
        width: 100%;
    }
    .cost{
        width: 100%;
    }
    .inout{
        width: 100%;
    }
</style>
